package student;

import java.io.FileNotFoundException;
import java.io.FileReader;

import student.productions.Program;

public class ParserConsole {

	public static void main(String[] args){
		CritterParser parser = new CritterParser();
		FileReader input;
		StringBuffer sb = new StringBuffer();
		try{
			if(args[0].equals("--mutate")){
				int mutations = Integer.parseInt(args[1]);
				input = new FileReader(args[2]);
				Program program = parser.parse(input);
				if(program.size() <= 1){
					System.out.println("the program must have at least one rule in it");
					return;
				}
				program.prettyPrint(sb);
				System.out.println(sb);
				for(int i = 0; i < mutations; i++){
					sb = new StringBuffer();
					System.out.println("\n");
					program = program.mutate();
					System.out.println("New program is: ");
					program.prettyPrint(sb);
					System.out.println(sb);
				}
			} else{//only arg is an input filename
				input = new FileReader(args[0]);
				Program program = parser.parse(input);
				program.prettyPrint(sb);
				System.out.println(sb);
			}
		} catch(NumberFormatException e){
			System.out.println("--mutate must be followed by an integer");
		} catch(ArrayIndexOutOfBoundsException e){
			System.out.println("Incorrect number of arguments for this command");
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
			System.out.println("first command must be --mutate or a file name.");
		}
	}
}
